import { deepClone, numberFormat } from '@/util/util'
export const rcaNumberFormat = (value, isFen) => {
  const val = Number(parseInt(value, 10))
  const finalValue = isNaN(val) ? 0 : val
  return numberFormat((isFen ? finalValue/100 : finalValue), 2)
}

export const rcaNumberFormat0 = (value, isFen) => {
  const val = Number(value)
  const finalValue = isNaN(val) ? 0 : val
  return numberFormat((isFen ? (finalValue/100) : finalValue), 2)
}
export const rcaNumberFormat4 = (value, isFen) => {
  const val = Number(value)
  const finalValue = isNaN(val) ? 0 : val
  return numberFormat((isFen ? finalValue/100 : finalValue), 2)
}
//保留2位
export const rcaNumberFormat00 = (value, isFen) => {
  const val = Number(value)
  const finalValue = isNaN(val) ? 0 : val
  return numberFormat((isFen ? (finalValue/100).toFixed(2) : finalValue.toFixed(2)), 2)
}
//保留4位
export const rcaNumberFormat0000 = (value, isFen) => {
  const val = Number(value)
  const finalValue = isNaN(val) ? 0 : val
  return numberFormat((isFen ? (finalValue/100).toFixed(4) : finalValue.toFixed(4)), 4)
}
//Vue或JS的浮点型乘除法无法精确计算
export const multiBigInt = (arg1, arg2) => {
  var m = 0;
  var s1 = arg1.toString();
  var s2 = arg2.toString();
  try {
      m += s1.split(".")[1].length;
  } catch (e) {}
  try {
      m += s2.split(".")[1].length;
  } catch (e) {}
  return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}
/**
    * 深度优先遍历树
    * 一个递归方法
    * @params tree:要转换的树结构数据
    * @params list:保存结果的列表结构数据，初始传list = []
    * @params parentId:当前遍历节点的父级节点id，初始为null(因为根节点无parentId)
    **/
export const toListDF = (tree, list) => {
  for (let i in tree) { //遍历最上层
    //将当前树放入list中
    var node = tree[i];
    list.push({
      ...node,
    });
    //如果有子结点,再遍历子结点
    if (node.children && node.children.length) {
      toListDF(node.children, list, node.id)  //递归
    }
  }
}

/**
 * 树转list
 */
export const treeToList = (treeList) => {
  let list = [...treeList];  //结果lsit
  treeList.forEach((tree) => {
    if (tree.children && tree.children.length) {
      toListDF(tree.children, list);  //遍历子树,并加入到list中.
    }
  })
  return list;
}

export const commonTableOption = {

  // 表格边框
  border: true,
  // 是否显示表格的斑马条纹
  stripe: true,
  // 表格列齐方式
  align: 'center',

  // 是否显示操作菜单栏（行内）
  menu: false,
  // 菜单栏对齐方式
  menuAlign: 'center',

  // 搜索菜单宽度
  searchMenuSpan: 6,

  // 是否为全屏Dialog
  dialogFullscreen: true,
  // 弹出表单的label宽度
  labelWidth: 160,

  // 是否显示表格序号
  index: true,
  indexLabel: '序号',

  // 新建按钮
  addBtn: false,
  // 勾选
  selection: false,
}

// 合计公共方法
export const summaryMethod = (param, tableOption) => {
  const { columns, data } = param;
  const copy = deepClone(tableOption)
  const columnsList = treeToList(copy.column)
  const sums = columns.map((column, index) => {
    if (index === 0) {
      return '合计';
    }
    const optionColumn = columnsList.find(c => c.prop === column.property) || {}

    if (!optionColumn.showSummary) {
      return '-'
    }

    const newData = data.map(d => {
      return d[column.property];
    })

    const total = newData.reduce((prev, curr) => {
      const value = Number(curr);
      if (!isNaN(value)) {
        return prev + Number(curr);
      } else {
        return prev;
      }
    }, 0);
    return optionColumn.formatter ? optionColumn.formatter(null, total, column.label, optionColumn) : total;

  });

  return sums;
}


/**
 * lu 2021-08-31
 * @param {*} DateFor 
 * @returns 
 */
// 时间格式化
export const format_date =  (DateFor) => {//全局函数1
  let year = DateFor.getFullYear()
  let month = DateFor.getMonth()  +  1
  let day = DateFor.getDate()
  if (month >= 1 && month <= 9) {
      month = "0" +  month;
  }
  if (day >= 0 && day <= 9) {
      day = "0"  +  day;
  }
  let tmp= year  +  '-'  +  month  +  '-'  +  day
  return tmp
};

// 获取月初, 获取 当前月 + gap 的月初(若gap为-1, 则获取上月初,若gap为1,则获取下月初
export const get_month_first =  (gap) => {
  let date1 = new Date();
  // 获取间隔月
  let gapMonth = date1.getMonth()  +  gap
  // 获取间隔月月初
  let gapMonthFirstDay=new Date(date1.getFullYear(),gapMonth,1);
  let firstDay = format_date(gapMonthFirstDay)
  //console.log(firstDay)
  return firstDay
};

// 获取月末, 获取 当前月 + gap 的月末(若gap为-1, 则获取上月末,若gap为1,则获取下月末)
export const get_month_last =  (gap) => {
  // gap = gap  +  1
  let date1 = new Date()
  // 获取下个月月初
  let gapMonth = date1.getMonth()
  gapMonth = gapMonth  +  1  +  gap

  let NextMonthFirstDay=new Date(date1.getFullYear(),gapMonth,1)
  NextMonthFirstDay = NextMonthFirstDay.getTime() - 1000*60*60*24
  let temp = new Date()
  temp.setTime(NextMonthFirstDay)
  let lastDay = format_date(temp)
  return lastDay
};

// 获取日期函数,若gap=0,则为系统日期,若不为0,则当期日期-gap(待参考)
export const get_date =  (gap)  => {
  let date = new Date()
  date = date.getTime() - 1000*60*60*24*gap
  let temp = new Date()
  temp.setTime(date)
  let day = format_date(temp)
  return day
};

// 时间戳转时间
export const timestamp_to_time =  (gap) => {
  let year=new Date(now).getFullYear()
  let month=new Date(now).getMonth() + 1
  let date=new Date(now).getDate()
  let hour=new Date(now).getHours()
  let minute=new Date(now).getMinutes()
  let second=new Date(now).getSeconds()
  return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second; 
  // alert(page)
};
